package nonDefaultPackage;

import java.sql.*;
import java.sql.Date;
import java.util.*;

public class DBManager {
	static String server = "localhost";
	 static String password = "malibu";
	 static String account = "root";
	 static String database = "mysql";
	 private static  Connection con;
	private static Connection con1;
	 private static DBManager db;
	 
	public static DBManager getInstance(){
		if(db==null) db = new DBManager();
		return db;
	}
	 
	private DBManager(){
		try {
			 Class.forName("com.mysql.jdbc.Driver");
			 con = DriverManager.getConnection("jdbc:mysql://" + server, account, password);
			 con1 = DriverManager.getConnection("jdbc:mysql://" + server, account, password);
		}
		 
		//es eqseption rom ar moxdes conector jar chaagdet web-inf_is lib foldershic
			  catch (ClassNotFoundException e) {
				  System.out.println("Class not found Exception!!!!!");
				  e.printStackTrace();
				  
			 } catch (SQLException e) {
				 System.out.println("SQL Exception");
			 e.printStackTrace();
			 }
	}
	
	
public Iterator getTeamPlayers(Team t,Championship ch) throws SQLException{		
		ArrayList<String>list= new ArrayList<String>();
		 Statement stmt = con.createStatement();
		 stmt.executeQuery("use " + database);
		 int teamId=t.getId();
		 //int championshipId=ch.getId();
		 ResultSet rs = stmt.executeQuery("Select player_id from teamPlayers where team_id="+teamId+" and championship_id=1;");	
		 System.out.println("while-mde");
		while(rs.next()){
			String playerID=rs.getString(1);
			int playerId= Integer.parseInt(playerID);
			Statement stmt1 = con1.createStatement();
			stmt1.executeQuery("use "+database);
			ResultSet rs2 = stmt1.executeQuery("Select * from players where player_id="+playerId+";");
			//System.out.println(rs2==null);
			if(rs2.next());
			String playerName=rs2.getString("player_name");
			System.out.println(playerName+" "+rs2==null);
			Date dabadebisTarigi=Date.valueOf(rs2.getString("date_of_birth"));
			System.out.println(playerName+" "+dabadebisTarigi);
			list.add(playerName);
		}
		return list.iterator();		
	}
/**	
	public static void main(String[] args) throws SQLException {
		DBManager db = DBManager.getInstance();
		Team t = new Team("Lasha");
		Championship ch = new Championship(null);
		Iterator<String> it = db.getTeamPlayers(t, ch);
		while(it.hasNext()){
			String s = it.next();
			System.out.println(s);
		}
	}
*/
}
